package com.longrise.android.byjk.plugins.tabthird.invoice;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.loadmore.LoadMoreView;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.widget.view.BBswipeRefreshLayout;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.AppUtil;
import com.longrise.common.utils.PrintLog;

/**
 * Created by Administrator on 2017/11/21.
 */

public class ConsumeDetailActivity2 extends BaseActivity2 implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    private RecyclerView mRcv;
    private BBswipeRefreshLayout mSwipeRefreshLayout;
    private ConsumeDetailRcvAdapter mAdapter;

    //订单id
    private String mOrderId;

    private int mPageNum = 0;   //第几页
    private int mSinglepageNum = 10;  //单次请求获取的item数量
    private boolean isFirstLoaded = false; //页面第一次有没有重新加载过数据
    private String state = "1";
    private int mCurrentBeansLen;

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_consume_detail;
    }

    @Override
    public void initView() {
        mRcv= (RecyclerView) findViewById(R.id.consume_detail_irv);
        mSwipeRefreshLayout = (BBswipeRefreshLayout) findViewById(R.id.consume_detail_swipelayout);
        getExtraData();
        initAdapter();
        initRec();
        getFirstData(mOrderId,mPageNum,mSinglepageNum,false,state);
    }

    @Override
    public void onToolbarBackClick() {
        finish();
    }

    public void initRec() {
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mRcv.setLayoutManager(new LinearLayoutManager(mContext));
        mRcv.setAdapter(mAdapter);
        setToolbarTitle(AppUtil.getString(R.string.buy_course));
    }

    private void initAdapter() {
        mAdapter = new ConsumeDetailRcvAdapter(false);
        mAdapter.setOnLoadMoreListener(this);
        mAdapter.setLoadMoreView(new LoadMoreView() {
            @Override
            public int getLayoutId() {
                return R.layout.item_loadmore_view;
            }

            @Override
            protected int getLoadingViewId() {
                return R.id.load_more_loading_view;
            }

            @Override
            protected int getLoadFailViewId() {
                return R.id.load_more_load_fail_view;
            }

            @Override
            protected int getLoadEndViewId() {
                return R.id.load_more_load_end_view;
            }
        });

    }

    private void getExtraData() {
        Intent extraIntent = getIntent();
        mOrderId = extraIntent.getStringExtra(ConsumeDetailActivity.ORDER_ID);
        PrintLog.e("ConsumeDetail2","mOrderId=:"+mOrderId);
    }

    @Override
    public void onRefresh() {
        mSwipeRefreshLayout.setRefreshing(true);
        getData(mOrderId,0,mSinglepageNum,false,state);
    }

    private void getFirstData(String mOrderId,int pageNum,int pagesize, boolean isLoadmore,String state) {

        if (!isFirstLoaded) {
            getData(mOrderId,pageNum,pagesize,isLoadmore,state);
            isFirstLoaded = true;
        }
    }

    private void getData(String mOrderId,int pageNum,int pageSize, final boolean isLoadmore,String state) {
        mPageNum = pageNum;
        EntityBean bean = new EntityBean();
        bean.set("orderid", mOrderId);
        bean.set("state", state);
        bean.set("pagenumber", pageNum);
        bean.set("pagesize", pageSize);
        EntityBean bean1 = new EntityBean();
        bean1.set("bean", bean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl,
                "bbt_pay_searchSubjectByOrderId", bean1, new LoadDataManager.OnRequestCallListener2() {

                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            EntityBean bean = (EntityBean) result;
                            parseBeans(bean, isLoadmore);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                        if (isLoadmore) {
                            mAdapter.loadMoreFail();
                        }
                    }

                    @Override
                    public void onFinished(String key, String service) {
                        mSwipeRefreshLayout.setRefreshing(false);
                    }

                });
    }


    private void parseBeans(EntityBean bean, boolean isLoadmore) {
        Integer resultstate = bean.getInt("resultstate");
        String resultdesc = bean.getString("resultdesc");
        EntityBean entityBean = (EntityBean) bean.get("result");
        if (resultstate == 1) {
            mPageNum = mPageNum+1;
            EntityBean[] resultBeans = entityBean.getBeans("subject");
            if (resultBeans == null) {
                mCurrentBeansLen = 0;
                resultBeans = new EntityBean[]{};
            } else {
                mCurrentBeansLen = resultBeans.length;
            }
            mAdapter.setDatas(resultBeans, isLoadmore);
            if (isLoadmore) {
                mAdapter.loadMoreComplete();
            }
            if (mCurrentBeansLen < mSinglepageNum) {
                mAdapter.loadMoreEnd();
            }
        } else {
            mCurrentBeansLen = 0;
            showToast(resultdesc);
            if (isLoadmore) {
                mPageNum = mPageNum-1;
                mAdapter.loadMoreFail();
            }
        }
    }

    @Override
    public void onLoadMoreRequested() {
        if (mCurrentBeansLen < mSinglepageNum) {
            //mAdapter.loadMoreEnd(false);
        } else {
            getData(mOrderId,mPageNum,mSinglepageNum,true,state);
        }
    }
}
